// 朋友圈数据API
const BASE_URL = '/media';

// 获取朋友圈数据
export const getPyqData = async (page = 1, limit = 12, status = '') => {
  try {
    const requestBody = {
      page,
      limit
    };
    
    // 如果选择了状态筛选，添加状态参数
    if (status !== '') {
      requestBody.is_use = parseInt(status);
    }
    
    const response = await fetch(`${BASE_URL}/article/get/pyq/data`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(requestBody)
    });

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json();
    return data;
  } catch (error) {
    console.error('获取朋友圈数据失败:', error);
    throw error;
  }
};

// 图片代理URL生成
export const getProxyImageUrl = (originalUrl) => {
  if (!originalUrl) return '';
  
  // 检查是否是有效的图片URL
  const isImageUrl = (url) => {
    // 微信图片链接
    if (url.includes('qpic.cn') || url.includes('wxapp.tc.qq.com')) {
      return true;
    }
    
    // 其他常见图片格式
    const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp'];
    const hasImageExtension = imageExtensions.some(ext => url.toLowerCase().includes(ext));
    
    // 检查是否是图片内容类型
    const isImageContent = url.includes('/image/') || url.includes('image/') || url.includes('img/');
    
    return hasImageExtension || isImageContent;
  };
  
  // 如果是微信图片链接，通过代理访问
  originalUrl = originalUrl.replace('http://', 'https://');
  if (originalUrl.includes('qpic.cn') || originalUrl.includes('wxapp.tc.qq.com') || originalUrl.includes('wx.qlogo.cn') || originalUrl.includes('wx.qq.com') || originalUrl.includes('wx.qq.cn')) {
    let url = 'http://35810169ff.qicp.vip:46758/media/proxy/image?url=' + originalUrl;
    return url;
  }
  
  // 如果是其他图片链接，直接返回
  if (isImageUrl(originalUrl)) {
    return originalUrl;
  }
  
  // 如果不是图片链接，返回空字符串或占位符
  console.warn('非图片链接:', originalUrl);
  return '';
}; 